﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.ComponentModel;
using FinPlusInterfaces;

namespace FinPlusCompView
{
    internal class TestElements : ObservableCollection<TestElement>
    {
        public TestElement this[string name, FileType fileType]
        {
            get 
            {
                var t = this.Where(f => f.TestName.Equals(name) && f.FileType.Equals(fileType)); 
                return t.Count() < 1 ? null : t.First(); 
            }
        }

        public Dictionary<string, TestElement> Index { get; set; }

        public TestElement Clone(TestElement from, FileType type, string testPath)
        {
            var path = string.Format("{0}\\{1}.{2}.{3}", testPath, from.TestName, from.Type, type.ToString());
            return this.Index.ContainsKey(path) ? this.Index[path] : this.Set(path);
        }

        public void Add(string[] files)
        {
            Index = new Dictionary<string, TestElement>();
            this.Reset();

            foreach (var file in files)
                Set(file);
        }

        public TestElement Set(string file)
        {
            var t = new TestElement(file);
            Index[t.FilePath] = t;
            this.Add(t);
            return t;
        }

        public void Remove(string filePath)
        {
            this.Remove(Index[filePath]);
            Index.Remove(filePath);
        }

        public void Reset()
        {
            foreach (var t in this)
                t.Reset();
        }

        public List<string> TestNames()
        {
            return this.Select(t => t.TestName).Distinct().ToList();
        }

        public string NextTestName(string name)
        {
            var nextName = "";

            string n = Regex.Match(name, @"^[^\d]+").Value;
            for (int x = 1; true; x++)
            {
                var t = this.Where(f => f.TestName == n + x).Count();
                if (t > 0) continue;
                nextName = n + x;
                break;
            }
            return nextName;
        }

        public FileType TestType(string name)
        {
            var t = this.Where(f => f.TestName.EndsWith(name) && f.FileType.Equals( FileType.service));
            if (t.Count() > 0) 
                return FileType.service;

            return FileType.view;
        }
    }

    internal sealed class TestElement : INotifyPropertyChanged
    {
        public string TestName { get; private set; }
        public FileType FileType { get; private set; }
        public string FileName { get; private set; }
        public string FilePath { get; private set; }
        public string Type { get; private set; }
        public DateTime LastWrite { get { return _lastWrite; } set { _lastWrite = value; NotifyPropertyChanged("LastWrite"); } }
        public string FirstDiffString { get { return _firstDiff > 0 ? string.Format("Pod {0} failed", _firstDiff) : "N/A"; } }
        public int FirstDiff { get { return _firstDiff; } set { _firstDiff = value; NotifyPropertyChanged("FirstDiffString"); NotifyPropertyChanged("FirstDiff"); } }
        public event PropertyChangedEventHandler PropertyChanged;

        private DateTime _lastWrite;
        private int _firstDiff;

        //public
        public TestElement(string file)
        {
            FileName = file.Substring(file.LastIndexOf('\\') + 1);
            var p = FileName.Split('.');
            TestName = p[0];
            Type = p[1];
            FileType = (FileType)Enum.Parse(typeof(FileType), p[2].ToLower(), true);
            FilePath = file;
            Reset();
        }

        public void Reset()
        {
            FirstDiff = FileType == FileType.state || FileType == FileType.cmds || FileType == FileType.query || FileType == FileType.data ? -1 : 0;
            LastWrite = File.GetLastWriteTime(FilePath);
        }

        public void NotifyPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}
